home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7154 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C beginner needs your help ASAP
  5. Date: Mon, 19 Feb 96 01:28:33 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <824693313snz@genesis.demon.co.uk>
  9. References: <4g862f$p0b@risky.ecs.umass.edu> <4g8ahd$p8b@spectator.cris.com>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <4g8ahd$p8b@spectator.cris.com>
  16.            aubrey@concentric.net "Aubrey Harrison" writes:
  17.  
  18. >In article <4g862f$p0b@risky.ecs.umass.edu>, sebag@ecs.umass.edu says...
  19. >>
  20. >>I am a new C programmmer who desperately needs help.
  21. >>I have been digging in the manuals for a way to do this but have still
  22. >>come out empty handed. Here is the problem: I want to open files in a while
  23. >>loop with different filenames. data0,data1,data2,data3, .. data1000 , ...
  24.  
  25. ...
  26.  
  27. >                sprintf(buf,"%d",i);
  28. >                strcpy( filename,"data");
  29. >                strcat( filename, buf );
  30. >                strcat( filename, ".ext");
  31.  
  32.  
  33. Can can replace these 4 lines with:
  34.  
  35.                  sprintf(filename, "data%d.ext", i);
  36.  
  37. (you'd scrap the .ext to match the spec).
  38.  
  39. sprintf is a very powerful function, for example
  40.  
  41.                  sprintf(filename, "data%03d", i);
  42.  
  43. can generate filenames of the form data000,data001,data002,...,data999 which
  44. is often preferable (e.g. for sorted directory listings).
  45.  
  46. -- 
  47. -----------------------------------------
  48. Lawrence Kirby | fred@genesis.demon.co.uk
  49. Wilts, England | 70734.126@compuserve.com
  50. -----------------------------------------
  51.